package com.controller.user;

import com.service.user.GoodService;
import com.service.user.LogService;
import com.utils.FileUploadUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

/** 响应主页请求 */
@RestController
@CrossOrigin
public class HomeController {
  @Autowired LogService logService;

  @Autowired GoodService goodService;

  /**
   * 获取主页展示的图片（如logo，背景等),同时添加网页浏览记录
   *
   * @param httpServletRequest http请求
   * @return 初始页面需要的图片
   */
  @GetMapping("home/getInitPic")
  public Map<String, String> getInitPic(HttpServletRequest httpServletRequest) {
    // 添加网页浏览记录
    logService.visitorCome(httpServletRequest);

    return FileUploadUtil.INIT_PIC_URL;
  }

  // 获取走马灯推荐图片
  @GetMapping("home/getRecommendGoods")
  public List<Map<String, String>> getRecommendGoods(@RequestParam("userId") String userId) {
    List<Map<String, String>> ret = goodService.getMostPopular(userId, 4);
    return ret;
  }

  /**
   * 获取主页分类展示书籍的列表
   *
   * @param categoryNum 获取的种类数目
   * @param goodsNum 每个类别的商品数量
   * @return 分类展示书籍的列表
   */
  @GetMapping("home/getShowGoods")
  public List getShowGoods(
      @RequestParam("categoryNum") int categoryNum, @RequestParam("goodsNum") int goodsNum) {
    List sgl = goodService.getShowGoods(categoryNum, goodsNum);
    return sgl;
  }

  /**
   * 退出网页，用以记录游客或用户的离开时间
   *
   * @param httpServletRequest http请求
   * @param userId 用户编号，游客为“”即可
   * @return 1
   */
  @GetMapping("exit")
  public String exit(HttpServletRequest httpServletRequest, @RequestParam("userId") String userId) {
    logService.visitorLeave(httpServletRequest);
    if (!userId.equals("")) {
      logService.userLeave(userId, httpServletRequest);
    }
    return "1";
  }
}
